﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Eugenhome.Atrium.Rss.Web
{
    /// <summary>
    /// 
    /// </summary>
    public partial class rssedit : System.Web.UI.Page
    {
        /// <summary></summary>
        public RssSource SelectedItem { get; set; }

        /// <summary></summary>
        private RssSourceCollection _rsc;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            _rsc = RssSourceCollection.Load(Server.MapPath("rss.xml"));

            SelectedItem = _rsc.RssSources.Where(item => item.ID.ToString().Equals(lstItem.SelectedValue)).FirstOrDefault();

            if (!IsPostBack)
            {
                UpdateListBox();

                lstProxy.DataSource = _rsc.ProxyAddresses;
                lstProxy.DataBind();
                lstProxy.SelectedValue = _rsc.ProxyAddress;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        private void UpdateListBox()
        {
            string selectedvalue = SelectedItem == null ? "" : SelectedItem.ID.ToString();

            lstItem.DataSource = _rsc.RssSources.Select(item => new RssSourceItem(item)).ToList();
            lstItem.DataTextField = "Name";
            lstItem.DataValueField = "ID";
            lstItem.DataBind();

            if (selectedvalue.Equals(string.Empty) && !lstItem.Items.Count.Equals(0))
            {
                lstItem.SelectedIndex = 0;
            }
            else
            {
                lstItem.SelectedValue = selectedvalue;
            }

            lstItem_SelectedIndexChanged(null, null);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void lstItem_SelectedIndexChanged(object sender, EventArgs e)
        {
            SelectedItem = _rsc.RssSources.Where(item => item.ID.ToString().Equals(lstItem.SelectedValue)).FirstOrDefault();
            this.DataBind();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSave_Click(object sender, ImageClickEventArgs e)
        {
            SelectedItem.Name = txtTitle.Text;
            SelectedItem.Url = txtLink.Text;
           _rsc.ProxyAddress= lstProxy.SelectedValue ;

            _rsc.Save();

            UpdateListBox();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnDelete_Click(object sender, ImageClickEventArgs e)
        {
            int nextSelectedIndex = lstItem.SelectedIndex < lstItem.Items.Count - 1 ? lstItem.SelectedIndex + 1 : lstItem.SelectedIndex - 1;
            string nextID = !nextSelectedIndex.Equals(-1) ? lstItem.Items[nextSelectedIndex].Value : "";

            _rsc.Delete(SelectedItem);

            SelectedItem = _rsc.RssSources.Where(item => item.ID.ToString().Equals(nextID)).FirstOrDefault();

            UpdateListBox();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnAdd_Click(object sender, ImageClickEventArgs e)
        {
            _rsc.Add("", "");

            SelectedItem = _rsc.RssSources.LastOrDefault();

            UpdateListBox();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnMoveDown_Click(object sender, EventArgs e)
        {
            MoveTab(lstItem.SelectedIndex, (!lstItem.SelectedIndex.Equals(lstItem.Items.Count - 1)) ? lstItem.SelectedIndex + 1 : 0);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnMoveUp_Click(object sender, EventArgs e)
        {
            MoveTab(lstItem.SelectedIndex, (!lstItem.SelectedIndex.Equals(0)) ? lstItem.SelectedIndex - 1 : lstItem.Items.Count - 1);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="currentIndex"></param>
        /// <param name="newIndex"></param>
        private void MoveTab(int currentIndex, int newIndex)
        {
            if (!newIndex.Equals(currentIndex))
            {
                _rsc.RssSources.Remove(SelectedItem);
                _rsc.RssSources.Insert(newIndex, SelectedItem);
                _rsc.Save();

                UpdateListBox();
            }

        }



    }
}